package org.example.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

@Component
public class MailUtils {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private TemplateEngine templateEngine;
    @Value("${spring.mail.username}")
    private String senderNumber;

    /**
     * 使用Thymeleaf做邮件模板发送
     * 引用spring-boot-starter-thymeleaf
     * @param title 邮件主题
     * @param acceptorNumber 接受者邮件号
     * @param checkCode 验证码
     * @return
     */
    public boolean sendCheckCodeEmail(String title,String acceptorNumber, String checkCode){
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//            System.out.println(senderNumber);
            helper.setFrom(senderNumber);	//邮件发送者
            helper.setSubject(title);	//邮件主题
            helper.setTo(acceptorNumber);		//接受者（可多个）
            helper.setSentDate(new Date());		//发送日期
//        helper.setCc("2154864828@qq.com");		//抄送人（可多个）
//		helper.setBcc("3292752137@qq.com");	//隐秘抄送人
            Context context = new Context();
            context.setVariable("checkCode",checkCode);
            String process = templateEngine.process("mail.html", context);
            helper.setText(process,true);	//邮件正文
            javaMailSender.send(mimeMessage);	//发送
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    /**
     * 简单邮件
     * @param title 邮件主题
     * @param acceptorNumber 接受者邮件号
     * @param mainString 邮件正文
     */
    public boolean sendSimpleMail(String title,String acceptorNumber, String mainString){
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(senderNumber + "(程序“猿”)");	//邮件发送者
            message.setSubject(title);	//邮件主题
            message.setTo(acceptorNumber);		//接受者（可多个）
            message.setSentDate(new Date());		//发送日期
            message.setText(mainString);	//邮件正文
            javaMailSender.send(message);	//发送
            return true;
        } catch (MailException e) {
            return false;
        }
    }

    /**
     * 带附件的邮件
     * @param title 邮件主题
     * @param acceptorNumber 接受者邮件
     * @param mainString 邮件正文
     * @param fileName 发送显示的文件名
     * @param File 发送的文件资源
     * @return
     */
    public boolean sendAttachMail(String title,String acceptorNumber, String mainString, String fileName, File File){
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(senderNumber);	//邮件发送者
            helper.setSubject(title);	//邮件主题
            helper.setTo(acceptorNumber);		//接受者（可多个）
            helper.setSentDate(new Date());		//发送日期
            helper.setText(mainString);	//邮件正文
            helper.addAttachment(fileName, File);   //附件名和附件文件
            javaMailSender.send(mimeMessage);	//发送
            return true;
        } catch (MessagingException e) {
//            e.printStackTrace();
            //邮件发送失败
            return false;
        }
    }

}
